<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\CatePostRequest;
use App\Http\Controllers\Controller;
use DB;


class CommentsController extends Controller
{
	//获取商品
	public function getList(Request $request){
        //获取商品每页显示记录条数
        $num = $request->input('num',10);
        //获取查询商品
        $search = $request->input('search');

        if($search){
            //如果是查询执行
            $goodsinfo = DB::table('hm_goods')
            ->join('hm_comments','hm_comments.gid','=','hm_goods.id')
            ->where('goodsname','like','%'.$search.'%')
            ->select('hm_goods.id','goodsname','contents','star')
            ->paginate($num);
        }else{
    		//获取商品列表,即评论
    		$goodsinfo = DB::table('hm_goods')
    				->join('hm_comments','hm_comments.gid','=','hm_goods.id')
    				->select('hm_goods.id','goodsname','contents','star')
    				->get();
        }
        $data = $request->all();
		//传递参数,解析模板
		return view('admin.comments.list',['goodsinfo'=>$goodsinfo,'data'=>$data]);
	}
    //商品信息
    public function getEdit(Request $request){
    	
	    	//存在评论,
	    	$goodsinfo = DB::table('hm_comments')
	    			->join('hm_goods','hm_goods.id','=','hm_comments.gid')
	    			->select('goodsname','gid','star')
	    			->where('hm_goods.id',$request->input('id'))
	    			->get();
        //解析模板
        return view('admin.comments.edit',['goodsinfo'=>$goodsinfo]);
    }

    //ajax获取商品评论
    public function getMessage(Request $request){
    	//从商品表获取评论
    	$messages = DB::table('hm_messages')
    				->join('hm_users','hm_users.id','=','hm_messages.uid')
    				->select('username','message','time','star','time','floor','gid','uid','hm_messages.id')
    				->where('gid',$request->input('id'))
    				->get();
    	//判断是否存在评论信息
    	if($messages){
    		echo json_encode($messages);
    	}else{
    		echo '0';
    	}
    }

    //ajax插入评论
    public function postInsert(Request $request){
        //获取用户数据
        $comments = $request->all();
        //var_dump($data);
        //拼接商品信息
        $data = ['gid'=>$comments['gid'],'message'=>$comments['info'],'time'=>time()];
        //var_dump($data);
        //插入数据
        $status = DB::table('hm_messages')->insert($data);
        if($status){
            echo $status;
        }
    }
    //ajax删除评论
    public function postDelete(Request $request){
        $id = $request->input('id');
        $status = DB::table('hm_messages')->where('id',$id)->delete();
        echo $status;
    }

    //ajax修改评论
    public function postAlert(Request $request){
        //接受修改信息数据
        $data = $request->only('id','message');
        var_dump($data['message']);
        //更新数据
        $status = DB::table('hm_messages')->where('id',$data['id'])->update($data);
        echo $status;
    }





























}
